package com.chezhujie.www.controller.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Trolepermission;
import com.chezhujie.www.service.TrolepermissionService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;




@Controller
public class TrolepermissionController /*extends BaseController*/{
	private static final Logger log = LoggerFactory.getLogger(TrolepermissionController.class);
	public TrolepermissionController() {
	}
 
 
 	/**
	 * 角色权限新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("trolepermission", new Trolepermission());
      setDict(model);
		return "trolepermissionEdit";
	}

	/**
	 * 角色权限保存
	 * 
	 * @param trolepermission
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionSave.do")
	public String save(Trolepermission trolepermission, HttpServletRequest request, ModelMap model) throws Exception{     
		try {
			if(trolepermission.getPermid()!=null&&!trolepermission.getPermid().equals("")){
				// 编辑
				trolepermissionService.updateTrolepermission(trolepermission);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑角色权限", "permid="+trolepermission.getPermid()));
			} else {
				// 新增
				trolepermissionService.insertTrolepermission(trolepermission);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增角色权限", "permid="+trolepermission.getPermid()));
			}
   		} catch (Exception e) {
   			log.error("trolepermission save error:", e); 
            throw e;
   		}
      
		return "redirect:trolepermissionList.do";
	}

	/**
	 * 角色权限删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("trolepermissionDelete.do")
	public String delete(@RequestParam("ids") List<String> permidList) throws Exception{
		try {
         if (permidList != null) {
   			trolepermissionService.deleteTrolepermission(permidList);
   		}
         } catch (Exception e) {
   			log.error("trolepermission delete error:", e); 
            throw e;
   		}
		return "redirect:trolepermissionList.do";
	}
	 
	/**
	 * 角色权限编辑页面
	 * 
	 * @param permid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionEdit.do")
	public String edit(Long permid, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("permid", permid);
         Trolepermission trolepermission = trolepermissionService.getTrolepermission(param);
   		model.addAttribute("trolepermission", trolepermission);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("trolepermission edit error:", e); 
            throw e;
   		}
		return "trolepermissionEdit";
	}

	/**
	 * 角色权限详情
	 * 
	 * @param permid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionDetail.do")
	public String detail(Long permid, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("permid", permid);
            Trolepermission trolepermission = trolepermissionService.getTrolepermission(param);
      		model.addAttribute("trolepermission", trolepermission);
            setDict(model);
            } catch (Exception e) {
   			log.error("trolepermission detail error:", e); 
            throw e;
   		  }
		return "trolepermissionView";
	}
	 
	/**
	 * 角色权限查询（分页）
	 * 
	 * @param trolepermission
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionList.do")
	public String listPage(Trolepermission trolepermission, Integer pageNo, String order,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
         Pagination pagination = trolepermissionService.getTrolepermissionList(toMap(trolepermission), 20, pageNo,
   				order, sort);          
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("trolepermission listPage error:", e); 
         throw e;
		   }
		return "trolepermissionList";
	}
	 
	/**
	 * 角色权限查询
	 * 
	 * @param trolepermission
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("trolepermissionList1.do")
	public String list(Trolepermission trolepermission, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = trolepermissionService.getTrolepermissionList(toMap(trolepermission));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("trolepermission list error:", e); 
            throw e;
   		}
		return "trolepermissionList";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param trolepermission
	 * @return
	 */	private Map<String,Object> toMap(Trolepermission trolepermission){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(trolepermission.getPermid()!=null&&!trolepermission.getPermid().equals("")){
			map.put("Permid", trolepermission.getPermid());
		}
		if(trolepermission.getRoleid()!=null&&!trolepermission.getRoleid().equals("")){
			map.put("Roleid", trolepermission.getRoleid());
		}
		if(trolepermission.getUri()!=null&&!trolepermission.getUri().equals("")){
			map.put("Uri", trolepermission.getUri());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}



   
      
	@Resource
	private TrolepermissionService trolepermissionService;


}